package com.gemme.gemme.wode;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.view.DingdanDeleteDialog;
import com.gemme.gemme.view.OnChangeLIstener;
import com.gemme.gemme.view.PayDialog;
import com.gemme.gemme.view.RefreshView;
import com.gemme.gemme.view.SgDDView;
import com.gemme.gemme.view.Topview;
import com.google.android.material.tabs.TabLayout;
import com.jaeger.library.StatusBarUtil;

import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

//侧滑删除地址
public class WoDingdanNewActivity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dingdan_new_wo);
        initTop();

        initView();

        initTab();
        initIntent();
    }

    private void initIntent() {
        try {
            int index = getIntent().getIntExtra("type",0);
            tablayout.getTabAt(index).select();//进行选择第几个tab
            if(index == 0){
                type = -1;
                getData();
            }

        }catch (Exception e){

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //回到主页
//        tablayout.getTabAt(0).select();//进行选择第几个tab
        getData();
    }

    RefreshView refreshView;
    Dingdan2Adapter dingdan2Adapter;
    private void initView() {

        refreshView = findViewById(R.id.refreshView);
        refreshView.setLayoutManager(new LinearLayoutManager(this));

        dingdan2Adapter = new Dingdan2Adapter();
        refreshView.setAdapter(dingdan2Adapter);
    }

    int type = 0; // 0-- 全部  1 -- 待付款
    TabLayout tablayout;
    private void initTab() {
         String[] titles = new String[]{"全部","待付款","待发货","待收货","待评价"};

        tablayout = findViewById(R.id.tablayout);

        for(int i=0;i<titles.length;i++){
            TabLayout.Tab tab = tablayout.newTab();
            tab.setTag(i);
            tablayout.addTab(tab);
            tablayout.getTabAt(i).setText(titles[i]);
        }

        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                System.out.println();
                String s = tab.getText() + "";
                switch (s){
                    case "全部":
                        type = -1;
                        break;

                    case "待付款":
                        type = 0;
                        break;

                    case "待发货":
                        type = 1;
                        break;

                    case "待收货":
                        type = 2;
                        break;

                    case "待评价":
                        type = 4;
                        break;
                }

//                type = (int) tab.getTag();
                getData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                System.out.println();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                System.out.println();
            }
        });
    }

    private void getData() {
        int orderStatus = type;
        String url;
        /*if(orderStatus >= 0){
         *//*   url = DataManager.BaseUrl_panghu + ":9999/mall/shoporderdetail/wholeOrder?" +
                    "orderStatus =" + orderStatus;*//*
            url = DataManager.BaseUrl_panghu + ":9999/mall/shoporderdetail/wholeOrder/" + orderStatus;
        }else{
//            url = DataManager.BaseUrl_panghu + ":9999/mall/shoporderdetail/wholeOrder";
            url = DataManager.BaseUrl_panghu + ":9999/mall/shoporderdetail/wholeOrder/"+ orderStatus;
        }
*/
        url = DataManager.BaseUrl_panghu + "mall/shoporderdetail/wholeOrder/"+ orderStatus;

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try {
                    System.out.println("");
                    DingdanListBean dingdanListBean = JSON.parseObject(response, DingdanListBean.class);
                    List<DingdanListBean.DataBean> data = dingdanListBean.getData();
                    if(data == null || data.size() == 0){
                        findViewById(R.id.nullView).setVisibility(View.VISIBLE);
                        refreshView.setVisibility(View.GONE);
                        return;
                    }else{
                        findViewById(R.id.nullView).setVisibility(View.GONE);
                        refreshView.setVisibility(View.VISIBLE);
                    }

                    if(dingdan2Adapter != null){
                        dingdan2Adapter.setData(data);
                        dingdan2Adapter.notifyDataSetChanged();
                    }


                }catch (Exception e){

                }
                System.out.println("");
            }
        });
    }

    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("我的订单");
    }


    public class Dingdan2Adapter extends RecyclerView.Adapter<Dingdan2Adapter.ViewHolder>{

        private List<DingdanListBean.DataBean> adapterList;

        @NonNull
        @Override
        public Dingdan2Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_dingdan2, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new Dingdan2Adapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull Dingdan2Adapter.ViewHolder holder, int position) {
            setAllItemGone(holder);
            try {
                DingdanListBean.DataBean dataBean = adapterList.get(position);
                int orderStatus = dataBean.getOrderStatus();
                switch (orderStatus){
                    case 0: //0代表待付款
                        holder.layout_1.setVisibility(View.VISIBLE);
                        initFukuan(holder.layout_1,position);
                        break;
                    case 1://代发货
                        holder.layout_2.setVisibility(View.VISIBLE);
                        initFahuo(holder.layout_2,position);
                        break;
                    case 2://待收货
                        holder.layout_3.setVisibility(View.VISIBLE);
                        initShouhuo(holder.layout_3,position);
                        break;
                    case 3://已取消
                        holder.layout_5.setVisibility(View.VISIBLE);
                        initYIQuxiao(holder.layout_5,position);
                        break;
                    case 4://待评价
                        holder.layout_4.setVisibility(View.VISIBLE);
                        initPingjia(holder.layout_4,position);
                        break;
                }

            }catch (Exception e){

            }



        }
//已取消,过期的
        private void initYIQuxiao(View view, int position) {
            TextView tv_delete = view.findViewById(R.id.tv_delete);
            tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    startActivity(new Intent(mContext,DDDfkActivity.class));
                    DingdanListBean.DataBean dataBean = adapterList.get(position);
                    int id = dataBean.getId();
                    OrderManager.deleteOrder(mContext, id, new OnChangeLIstener() {
                        @Override
                        public void onChangeLIstener() {
                            type = -1;
                            //getData();//重新刷新数据
                            tablayout.getTabAt(0).select();//回到全部
                            getData(); //获取全部数据

                        }
                    });
                }
            });

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DingdanListBean.DataBean dataBean = adapterList.get(position);
                    int id = dataBean.getId();
                    Intent intent = new Intent(mContext, DDCancleActivity.class);
                    intent.putExtra("data",id);
                    startActivity(intent);
                }
            });

            initSgView(view,position);
        }

        //付款
        private void initFukuan(View view,int position) {
            TextView tv_fukuan = view.findViewById(R.id.tv_fukuan);
            TextView tv_cancle = view.findViewById(R.id.tv_cancle);
            tv_fukuan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                  /*  DingdanListBean.DataBean dataBean = adapterList.get(position);
                    int id = dataBean.getId();
                    Intent intent = new Intent(mContext, DDDfkActivity.class);
                    intent.putExtra("data",id);
                    startActivity(intent);*/
                    DingdanListBean.DataBean dataBean = adapterList.get(position);
                    if(dataBean == null)
                        return;
                    String orderCode = dataBean.getOrderId();
                    double orderSubtotal = dataBean.getPaymentMoney();
                    String refundReason = "";
                    String orderDetailId = dataBean.getId() + "";
                    PayDialog payDialog = new PayDialog();
                    payDialog.setData(orderCode,orderSubtotal,refundReason,orderDetailId);
                    payDialog.showDialog(mContext);
                    payDialog.setOnChangeLIstener(new OnChangeLIstener() {
                        @Override
                        public void onChangeLIstener() {
                            finish();
                        }
                    });
                }
            });

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DingdanListBean.DataBean dataBean = adapterList.get(position);
                    int id = dataBean.getId();
                    Intent intent = new Intent(mContext, DDDfkActivity.class);
                    intent.putExtra("data",id);
                    startActivity(intent);
                }
            });

            tv_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DingdanListBean.DataBean dataBean = adapterList.get(position);
                    int id = dataBean.getId();
                    OrderManager.cancleOrder(mContext, id, new OnChangeLIstener() {
                        @Override
                        public void onChangeLIstener() {
                            System.out.println("");
                            getData();
                        }
                    });
                }
            });

            initSgView(view,position);
        }

        //发货
        private void initFahuo(View view,int position) {
            TextView tv_refund = view.findViewById(R.id.tv_refund);
            TextView tv_dfh = view.findViewById(R.id.tv_dfh);
            tv_refund.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DingdanListBean.DataBean dataBean = adapterList.get(position);
                    if(dataBean == null){
                        Toast.makeText(mContext, "退款失败", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    int id = dataBean.getId();
                    Intent intent = new Intent(mContext, RefundSelectActivity.class);
                    intent.putExtra("data",id);
                    startActivity(intent);
                }
            });
            tv_dfh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    startActivity(new Intent(mContext, DDDfhActivity.class));
                    Toast.makeText(WoDingdanNewActivity.this, "催发货成功", Toast.LENGTH_SHORT).show();
                }
            });

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DingdanListBean.DataBean dataBean = adapterList.get(position);
                    int id = dataBean.getId();
                    Intent intent = new Intent(mContext, DDDfhActivity.class);
                    intent.putExtra("data",id);
                    startActivity(intent);
                }
            });



            initSgView(view,position);
        }

        //收货
        private void initShouhuo(View view,int position) {
            TextView tv_tuikuan = view.findViewById(R.id.tv_tuikuan);
            TextView tv_wuliu = view.findViewById(R.id.tv_wuliu);
            TextView tv_shouhuo = view.findViewById(R.id.tv_shouhuo);

            tv_tuikuan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(mContext,RefundSelectActivity.class));
                }
            });

            tv_wuliu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DingdanListBean.DataBean dataBean = adapterList.get(position);
                    String courierServicesCode = dataBean.getCourierServicesCode();
                    int id = dataBean.getId();
                    if(TextUtils.isEmpty(courierServicesCode)){
                        Toast.makeText(WoDingdanNewActivity.this, "物流为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Intent intent = new Intent(mContext, SeeWuliuActivity.class);
                    intent.putExtra("courierServicesCode",courierServicesCode);

                    startActivity(intent);
                }
            });

            tv_shouhuo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    startActivity(new Intent(mContext,DingdanDetailActivity.class));
                }
            });

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DingdanListBean.DataBean dataBean = adapterList.get(position);
                    int id = dataBean.getId();
                    Intent intent = new Intent(mContext, DingdanDetailActivity.class);
                    intent.putExtra("data",id);
                    startActivity(intent);
                }
            });

            initSgView(view,position);
        }



        private void initPingjia(View view,int position) {
            TextView tv_pingjia = view.findViewById(R.id.tv_pingjia);
            View iv_delete = view.findViewById(R.id.iv_delete);
            tv_pingjia.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DingdanListBean.DataBean dataBean = adapterList.get(position);
                    int id = dataBean.getId();
                    Intent intent = new Intent(mContext, EvaluateActivity.class);
                    intent.putExtra("data",id);
                    startActivity(intent);
                }
            });

            iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new DingdanDeleteDialog().showDialog(mContext);
                }
            });


            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DingdanListBean.DataBean dataBean = adapterList.get(position);
                    int id = dataBean.getId();
                    Intent intent = new Intent(mContext, DpjDetailActivity.class);
                    intent.putExtra("data",id);
                    startActivity(intent);
                }
            });
            initSgView(view,position);


        }

        private void initSgView(View view,int position) {
            TextView tv_brand_name = view.findViewById(R.id.tv_brand_name);
            SgDDView sgDDView = view.findViewById(R.id.sgDDView);

            if(adapterList == null || adapterList.get(position) == null)
                return;

            try {
                DingdanListBean.DataBean dataBean = adapterList.get(position);
                tv_brand_name.setText(dataBean.getBrandName());
                sgDDView.setdata(dataBean);
            }catch (Exception e){

            }
        }

        @Override
        public int getItemCount() {
            return adapterList == null ? 0 : adapterList.size();

        }


        public void setAllItemGone(Dingdan2Adapter.ViewHolder holder){
            holder.layout_1.setVisibility(View.GONE);
            holder.layout_2.setVisibility(View.GONE);
            holder.layout_3.setVisibility(View.GONE);
            holder.layout_4.setVisibility(View.GONE);
            holder.layout_5.setVisibility(View.GONE);
        }

        public void setData(List<DingdanListBean.DataBean> adapterList) {

            this.adapterList = adapterList;
        }


        public class ViewHolder extends RecyclerView.ViewHolder {
            View layout_1;
            View layout_2;
            View layout_3;
            View layout_4;
            View layout_5;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                layout_1 = itemView.findViewById(R.id.layout_1);
                layout_2 = itemView.findViewById(R.id.layout_2);
                layout_3 = itemView.findViewById(R.id.layout_3);
                layout_4 = itemView.findViewById(R.id.layout_4);
                layout_5 = itemView.findViewById(R.id.layout_5);





            }
        }
    }

}